<?php

namespace app\api\controller;

use think\Lang;
/**
 * ============================================================================
 * DSMall多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 举报控制器
 */
class  Memberinform extends MobileMember {

    public function _initialize() {
        parent::_initialize(); // TODO: Change the autogenerated stub
        Lang::load(APP_PATH . 'home/lang/'.config('default_lang').'/memberinfom.lang.php');
    }


    /**
     * @api {POST} api/Memberinform/index 获取当前用户的举报列表
     * @apiVersion 1.0.0
     * @apiGroup Memberinform
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     * 
     * @apiParam {Int} page 页码
     * @apiParam {Int} per_page 每页数量
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     * @apiSuccess {Object[]} result.inform_list  举报列表
     * @apiSuccess {String} result.inform_list.inform_content  举报内容
     * @apiSuccess {Int} result.inform_list.inform_datetime  举报时间
     * @apiSuccess {Int} result.inform_list.inform_goods_id  商品ID
     * @apiSuccess {String} result.inform_list.inform_goods_image  商品图片名称
     * @apiSuccess {String} result.inform_list.inform_goods_image_url  商品图片完整路径
     * @apiSuccess {String} result.inform_list.inform_goods_name  商品名称
     * @apiSuccess {Int} result.inform_list.inform_handle_datetime  处理时间
     * @apiSuccess {Int} result.inform_list.inform_handle_member_id  处理用户ID
     * @apiSuccess {String} result.inform_list.inform_handle_message  处理描述
     * @apiSuccess {Int} result.inform_list.inform_handle_type  举报处理结果(1无效举报/2恶意举报/3有效举报)
     * @apiSuccess {Int} result.inform_list.inform_id  举报ID
     * @apiSuccess {Int} result.inform_list.inform_member_id  举报用户ID
     * @apiSuccess {String} result.inform_list.inform_member_name  举报用户名称
     * @apiSuccess {String} result.inform_list.inform_pic1  举报凭证1
     * @apiSuccess {String} result.inform_list.inform_pic2  举报凭证2
     * @apiSuccess {String} result.inform_list.inform_pic3  举报凭证3
     * @apiSuccess {Int} result.inform_list.inform_state  举报状态(1未处理/2已处理)
     * @apiSuccess {Int} result.inform_list.inform_store_id  店铺ID
     * @apiSuccess {String} result.inform_list.inform_store_name  店铺名称
     * @apiSuccess {String} result.inform_list.informsubject_content  举报主题内容
     * @apiSuccess {Int} result.inform_list.informsubject_id  举报主题ID
     * @apiSuccess {Int} result.inform_list.informsubject_state  举报主题状态 1:可用 2:失效
     * @apiSuccess {Int} result.inform_list.informsubject_type_id  举报类型ID
     * @apiSuccess {String} result.inform_list.informsubject_type_name  举报类型名称
     * @apiSuccess {Int} result.page_total  总页数
     * @apiSuccess {Boolean} result.hasmore  是否有更多 true是false否
     */
    public function index() {
        /*
         * 得到当前用户的举报列表
         */
        $inform_model = model('inform');
        $condition = array();
        $inform_state = intval(input('param.select_inform_state'));
        if($inform_state>0){
            $condition['inform.inform_state'] = $inform_state;
        }
        $condition['inform.inform_member_id'] = $this->member_info['member_id'];
        $inform_list = $inform_model->getInformList($condition, 10);
        foreach($inform_list as $key => $val){
            $inform_list[$key]['inform_goods_image_url']=goods_cthumb($val['inform_goods_image']);
        }
        $result = array_merge(array('inform_list' => $inform_list), mobile_page($inform_model->page_info));
        ds_json_encode(10000, '', $result);
    }


    /**
     * @api {POST} api/Memberinform/inform_submit 获取新增/编辑举报公共信息
     * @apiVersion 1.0.0
     * @apiGroup Memberinform
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     * 
     * @apiParam {Int} goods_id 商品ID
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     * @apiSuccess {Object} result.goods_info  举报商品信息
     * @apiSuccess {Object[]} result.type_list  举报类型列表
     * @apiSuccess {String} result.type_list.informtype_desc  举报类型描述
     * @apiSuccess {Int} result.type_list.informtype_id  举报类型ID
     * @apiSuccess {String} result.type_list.informtype_name  举报类型名称
     * @apiSuccess {Int} result.type_list.informtype_state  举报类型状态 1:有效 2:失效
     */
    public function inform_submit() {

        //检查当前用户是否允许举报
        $this->check_member_allow_inform();

        $goods_id = intval(input('param.goods_id'));

        //获取商品详细信息
        $goods_info = $this->get_goods_info_byid($goods_id);



        $inform_model = model('inform');
        //检查是否当前正在举报
        if ($inform_model->isProcessOfInform($goods_id)) {
            ds_json_encode(10001,lang('inform_handling'));
        }

        //获取举报类型
        $informsubjecttype_model = model('informsubjecttype');
        $inform_subject_type_list = $informsubjecttype_model->getActiveInformsubjecttypeList();
        if (empty($inform_subject_type_list)) {
            ds_json_encode(10001,lang('inform_type_null'));
        }

        ds_json_encode(10000, '', array('goods_info' => $goods_info,'type_list'=>$inform_subject_type_list));
    }


    /**
     * @api {POST} api/Memberinform/inform_save 保存用户提交的商品举报
     * @apiVersion 1.0.0
     * @apiGroup Memberinform
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     * 
     * @apiParam {Int} inform_goods_id 商品ID
     * @apiParam {String} inform_subject 举报主题
     * @apiParam {String} inform_content 举报内容
     * @apiParam {Array} pic_name 举报凭证 用,分隔多个举报凭证
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     */
    public function inform_save() {

        //检查当前用户是否允许举报
        $this->check_member_allow_inform();

        $goods_id = intval(input('post.inform_goods_id'));

        //获取商品详细信息
        $goods_info = $this->get_goods_info_byid($goods_id);

      

        //实例化举报模型
        $inform_model = model('inform');
        //检查是否当前正在举报
        if ($inform_model->isProcessOfInform($goods_id)) {
            ds_json_encode(10001,lang('inform_handling'));
        }
        //处理用户输入的数据
        $input = array();
        $input['inform_member_id'] = $this->member_info['member_id'];
        $input['inform_member_name'] = $this->member_info['member_name'];
        $input['inform_goods_id'] = $goods_id;
        $input['inform_goods_name'] = $goods_info['goods_name'];
        $input['inform_goods_image'] = $goods_info['goods_image'];
        list($input['informsubject_id'], $input['informsubject_content']) = explode(",", trim(input('post.inform_subject')));
        $input['inform_content'] = trim(input('post.inform_content'));

        $pic_name = input('post.pic_name/a');

        $input['inform_pic1'] = isset($pic_name[0]) ? $pic_name[0] : '';
        $input['inform_pic2'] = isset($pic_name[1]) ? $pic_name[1] : '';
        $input['inform_pic3'] = isset($pic_name[2]) ? $pic_name[2] : '';

        $input['inform_datetime'] = time();
        $input['inform_state'] = 1;
        $input['inform_handle_message'] = '';
        $input['inform_handle_member_id'] = 0;
        $input['inform_handle_datetime'] = 1;

        //验证输入的数据
        $data = [
            'inform_content' => $input["inform_content"],
            'informsubject_content' => $input["informsubject_content"]
        ];

        $inform_validate = validate('inform');
        if (!$inform_validate->scene('inform_save')->check($data)) {
            ds_json_encode(10001,$inform_validate->getError());
        }

        //保存
        if ($inform_model->addInform($input)) {
            ds_json_encode(10000,lang('inform_success'),url('Memberinform/index'));
        } else {
            ds_json_encode(10001,lang('inform_fail'), url('Memberinform/index'));
        }
    }


    /**
     * @api {POST} api/Memberinform/inform_cancel 取消用户提交的商品举报
     * @apiVersion 1.0.0
     * @apiGroup Memberinform
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     * 
     * @apiParam {Int} inform_id 举报ID
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     */
    public function inform_cancel() {

        $inform_id = intval(input('param.inform_id'));
        $inform_info = $this->get_inform_info($inform_id);

        if (intval($inform_info['inform_state']) === 1) {
            $pics = array();
            if (!empty($inform_info['inform_pic1'])) {
                $pics[] = $inform_info['inform_pic1'];
            }
            if (!empty($inform_info['inform_pic2'])) {
                $pics[] = $inform_info['inform_pic2'];
            }
            if (!empty($inform_info['inform_pic3'])) {
                $pics[] = $inform_info['inform_pic3'];
            }
            $this->drop_inform($inform_id, $pics);
            ds_json_encode(10000,lang('inform_cancel_success'));
        } else {
            ds_json_encode(10001,lang('inform_cancel_fail'));
        }
    }

    /**
     * @api {POST} api/Memberinform/inform_info 商品举报详细
     * @apiVersion 1.0.0
     * @apiGroup Memberinform
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     * 
     * @apiParam {Int} inform_id 举报ID
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     * @apiSuccess {Object} result.goods_info  商品信息 （返回信息参考goods）
     * @apiSuccess {Object} result.inform_info  举报信息 （返回字段参考inform）
     * @apiSuccess {String[]} result.inform_pic  举报凭证列表
     * @apiSuccess {Object} result.subject_info  举报主题信息
     * @apiSuccess {String} result.subject_info.informsubject_content  举报主题内容
     * @apiSuccess {Int} result.subject_info.informsubject_id  举报主题ID
     * @apiSuccess {Int} result.subject_info.informsubject_state  举报主题状态
     * @apiSuccess {Int} result.subject_info.informsubject_type_id  举报类型ID
     * @apiSuccess {String} result.subject_info.informsubject_type_name  举报类型名称
     */
    public function inform_info() {

        $inform_id = intval(input('param.inform_id'));
        $inform_info = $this->get_inform_info($inform_id);
        switch($inform_info['inform_handle_type']){
            case 1:
                $inform_info['inform_handle_type_text']=lang('inform_handle_type_unuse');
                break;
            case 2:
                $inform_info['inform_handle_type_text']=lang('inform_handle_type_venom');
                break;
            case 3:
                $inform_info['inform_handle_type_text']=lang('inform_handle_type_valid');
                break;
            
        }
        // 商品信息
        $goods_info = model('goods')->getGoodsInfoByID($inform_info['inform_goods_id']);
        // 投诉类型
        $subject_info = model('informsubject')->getOneInformsubject(array('informsubject_id' => $inform_info['informsubject_id']));

        $inform_pic = array();
        for ($i = 1; $i <= 3; $i++) {
            if (!empty($inform_info['inform_pic' . $i])) {
                $inform_pic[] = UPLOAD_SITE_URL . DS . ATTACH_PATH . DS . 'inform' . DS . $inform_info['inform_pic' . $i];
            }
          
        }
        ds_json_encode(10000, '', array('inform_pic' => $inform_pic,'goods_info' => $goods_info,'inform_info' => $inform_info,'subject_info'=>$subject_info));
    }

    /*
     * 根据id获取投诉详细信息
     */

    private function get_inform_info($inform_id) {

        if (empty($inform_id)) {
            ds_json_encode(10001,lang('param_error'));
        }

        $inform_model = model('inform');
        $inform_info = $inform_model->getOneInform(array('inform_id'=>$inform_id));

        if (empty($inform_info)) {
            ds_json_encode(10001,lang('param_error'));
        }

        if (intval($inform_info['inform_member_id']) !== intval($this->member_info['member_id'])) {
            ds_json_encode(10001,lang('param_error'));
        }

        return $inform_info;
    }

    /*
     * 根据id获取投诉详细信息
     */

    private function drop_inform($inform_id, $inform_pics) {

        $inform_model = model('inform');
        //删除图片
        if (!empty($inform_pics)) {
            foreach ($inform_pics as $pic) {
                $this->inform_delete_pic($pic);
            }
        }
        $inform_model->delInform(array('inform_id' => $inform_id));
    }

    /*
     * 根据id获取商品详细信息
     */

    private function get_goods_info_byid($goods_id) {

        if (empty($goods_id)) {
            ds_json_encode(10001,lang('param_error'));
        }

        $goods_model = model('goods');
        $goods_info = $goods_model->getGoodsOnlineInfoByID($goods_id);

        //检查该商品是否存在
        if (empty($goods_info)) {
            ds_json_encode(10001,lang('goods_null'));
        }

        return $goods_info;
    }

    /*
     * 检查当前用户是否允许举报
     */

    private function check_member_allow_inform() {

        //检查是否允许举报
        $member_model = model('member');
        if (!$member_model->isMemberAllowInform($this->member_info['member_id'])) {
            ds_json_encode(10001,lang('deny_inform'));
        }
    }

 
    /**
     * @api {POST} api/Memberinform/upload_pic 上传用户提供的举报图片
     * @apiVersion 1.0.0
     * @apiGroup Memberinform
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     *
     * @apiParam {File} inform_pic 举报凭证
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     * @apiSuccess {String} result.file_name  文件名称
     * @apiSuccess {String} result.pic  文件完整路径
     */ 
    public function upload_pic() {
        if (!empty($_FILES['inform_pic']['name'])) {
            $upload = request()->file('inform_pic');
            $uploaddir = BASE_UPLOAD_PATH .DS. 'home'.DS.'inform'.DS;
            $file_name = $this->member_info['member_id'] . '_' . date('YmdHis') . rand(10000, 99999);
            $result = $upload->rule('uniqid')->validate(['ext' => ALLOW_IMG_EXT])->move($uploaddir,$file_name);
            if ($result) {
                $file_name = $result->getFilename();
            } else {
                ds_json_encode(10001, '图片上传失败');
            }
            $pic = UPLOAD_SITE_URL . DS . ATTACH_PATH . DS . 'inform' . DS . $file_name;
            ds_json_encode(10000, '', array('file_name' => $file_name, 'pic' => $pic));
        }else{
            ds_json_encode(10001, '没选择图片');
        }
        
    }

    /*
     * 上传用户提供的举报图片
     */

    private function inform_delete_pic($pic_name) {

        //上传路径
        $pic = BASE_UPLOAD_PATH . DS . ATTACH_PATH . DS . 'inform' . DS . $pic_name;
        if (file_exists($pic)) {
            @unlink($pic);
        }
    }


    /**
     * @api {POST} api/Memberinform/get_subject_by_typeid 根据举报类型id获取，举报具体列表
     * @apiVersion 1.0.0
     * @apiGroup Memberinform
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     *
     * @apiParam {Int} type_id 类型ID
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     * @apiSuccess {Object[]} result.inform_subject_list  举报主题列表
     * @apiSuccess {String} result.inform_subject_list.informsubject_content  举报主题内容
     * @apiSuccess {Int} result.inform_subject_list.informsubject_id  举报主题ID
     */ 
    public function get_subject_by_typeid() {
        $informsubject_type_id = intval(input('param.type_id'));

        if (empty($informsubject_type_id)) {
            ds_json_encode(10001, lang('param_error'));
        } else {
            /*
             * 获得举报主题列表
             */
            $informsubject_model = model('informsubject');

            //搜索条件
            $condition = array();
            $condition['informsubject_type_id'] = $informsubject_type_id;
            $condition['informsubject_state'] = 1;
            $inform_subject_list = $informsubject_model->getInformsubjectList($condition, 10, 'informsubject_id,informsubject_content');

            ds_json_encode(10000, '', array('inform_subject_list' => $inform_subject_list));
        }
    }

  

}
